<?php

/* 
 * 接受數據的
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class inController extends NotController
{
    public function __construct() {
        parent::__construct();        
    }
    /**
     * 用于js传输
     */
    public function js(){
        if(isset($_GET['appid'])){
            $appid = trim($_GET['appid']);
            $row = CodeModel::load()->get_infoByAppid($appid);
            if($row) {
                $code_id = $row['code_id'];
                $user_id = $row['user_id'];              
                CodeModel::load()->update_last_time($code_id);       
                $result = $_POST;
                if(count($result) > 0) {
                    $ser_data = serialize($result);
                    $in_time = p_getTime();
                    $data = array(
                        'user_id' => $user_id,
                        'code_id' => $code_id,
                        'in_time' => $in_time,
                        'content' => $ser_data,
                    );
                    $bool = OrderInfoModel::load()->insert($data);                    
                    $this->ret['status'] = 0;
                    $this->ret['msg'] = 'ok';
                }                        
            }
        }         
        echo json_encode($this->ret);exit;
    }
    /**
     * 用于php传输
     */
    public function php(){
        if(isset($_REQUEST['appid'])){
            $appid = trim($_REQUEST['appid']);
            $row = CodeModel::load()->get_infoByAppid($appid);
            if($row) {
                $code_id = $row['code_id'];
                $user_id = $row['user_id'];
                $appkey = $row['appkey'];
                $api = new ApiBase($appkey);
                $result = $api->receive();                
                if($result === false) {
                    echo $api->setReturn(110, 'sign error');exit;
                } else {
                    CodeModel::load()->update_last_time($code_id);
                    $en = unserialize($result);
                    $json_str = serialize($en);
                    $data = array(
                        'user_id' => $user_id,
                        'in_time' => p_getTime(),
                        'content' => $json_str,
                    );
                    $bool = OrderInfoModel::load()->insert($data);
                    if($bool) {
                        echo $api->setReturn(0, 'ok');exit;
                    } else {
                        echo $api->setReturn(111, 'insert error');exit;
                    }
                }                
            }
        } 
        $this->ret['status'] = -1;
        $this->ret['data'] = 'error';
        echo json_encode($this->ret);
    }
}

